package interview.huawei.tjt.training.mianshi;

/**
 * 给一个二维数组，如
 * [1,3,1],
 * [1,5,1],
 * [4,2,2]
 * 从位置[0][0]开始，只能往右或者往下走，求出路径最小的和
 */
// 动态规划
public class Demo2 {

    public static void main(String[] args) {
        int[][] nums = new int[][]{{1,3,1},{1,5,1},{4,2,2}};
        int[][] dis = new int[3][3];
        dis[0][0] = nums[0][0];
        for (int i = 1; i < 3; i ++) {
            dis[0][i] = nums[0][i] + dis[0][i - 1];
            dis[i][0] = nums[i][0] + dis[i - 1][0];
        }

        for (int i = 1; i < 3; i ++) {
            for (int j = 1; j < 3; j ++) {
                dis[i][j] = Math.min(nums[i][j] + dis[i - 1][j], nums[i][j] + dis[i][j - 1]);
            }
        }
//        for (int i = 0; i < 3; i ++) {
//            for (int j = 0; j < 3; j ++) {
//                System.out.print(dis[i][j]);
//            }
//        }
        System.out.println(dis[2][2]);
    }


}
